﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Guolian.ProjectName.Infrastruct;
using Guoling.ProjectName.Data;
using Guoling.ProjectName.Model;
using Microsoft.Extensions.Logging;

namespace Guoling.ProjectName.Application
{
    public class UserAppService : IUserAppService
    {
        private readonly IUserRepository userRepository;
        private readonly ILogger<UserAppService> logger;
        public UserAppService(IUserRepository userRepository, ILoggerFactory loggerFactory)
        {
            this.userRepository = userRepository;
            logger = loggerFactory.CreateLogger<UserAppService>();
        }
        public async Task<bool> Add(UserViewModel userViewModel)
        {
            try
            {
                var user = await userRepository.AddAsync(new User
                {
                    Name = userViewModel.Name,
                    Gender = userViewModel.Gender,
                    Tel = userViewModel.Tel,
                });
                if (user != null)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return false;
            }
        }
        public async Task<IEnumerable<UserViewModel>> GetAll()
        {
            try
            {
                var users = await userRepository.GetAllAsync();

                return users.Select(x => new UserViewModel
                {
                    Id = x.Id,
                    Name = x.Name,
                    Gender = x.Gender,
                    Tel = x.Tel
                });

            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return Array.Empty<UserViewModel>();
            }
        }

        public Task<UserViewModel> GetById(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<bool> Remove(int id)
        {
            try
            {
                var state = await userRepository.DeleteByIdAsync(id);
                return true;
            }
            catch (Exception ex)
            {

                logger.LogError(ex.Message);
                return false;
            }
        }

        public Task<bool> RemoveRange(IEnumerable<int> ids)
        {
            throw new NotImplementedException();
        }

        public async Task<bool> Update(UserViewModel userViewModel)
        {
            try
            {
                var user = await userRepository.GetByIdAsync(userViewModel.Id);
                if (user.Name != userViewModel.Name) user.Name = userViewModel.Name;
                if (user.Gender != userViewModel.Gender) user.Gender = userViewModel.Gender;
                if (user.Tel != userViewModel.Tel) user.Tel = userViewModel.Tel;
                await userRepository.UpdateAsync(user);
                return true;
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return false;
            }
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

    }
}
